/* https://v0.dev/chat/Gkyl9XlKKO4 */


function addTexturedPlane(obj: {
    imgURL: string;
    width: number;
    height: number;
    x: number;
    y: number;
    z: number;
    renderOrder?: number;
}) {
    const textureLoader = new THREE.TextureLoader();
    const texture = textureLoader.load(obj.imgURL, function (texture) {
        texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
        texture.minFilter = THREE.LinearFilter;
        texture.magFilter = THREE.LinearFilter;
        texture.generateMipmaps = true;
        texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;

        // Approach 1: Use sRGB color space
        //texture.encoding = THREE.sRGBEncoding;

        const cur_xishu = 0.02; // 缩小的系数
        const geometry = new THREE.PlaneGeometry(obj.width * cur_xishu, obj.height * cur_xishu);

        // Approach 2: Adjust material color
        // const material = new THREE.MeshBasicMaterial({
        //   map: texture,
        //   transparent: true,
        //   opacity: 1,
        //   side: THREE.DoubleSide,
        //   blending: THREE.NormalBlending,
        //   color: new THREE.Color(0.95, 0.95, 0.95), // Slightly darken the material
        // });

        // const plane = new THREE.Mesh(geometry, material);
        // plane.material.depthWrite = false;

        // plane.position.set(obj.x, obj.y, obj.z);
        // plane.renderOrder = obj.renderOrder || 0;

        // scene.add(plane);

        // Approach 3: Custom shader for color correction
        const customMaterial = new THREE.ShaderMaterial({
            uniforms: {
                texture: { value: texture },
                correction: { value: new THREE.Vector3(0.8901960784313725, 0.8901960784313725, 0.9254901960784314) }, // RGB correction
            },
            vertexShader: `
        varying vec2 vUv;
        void main() {
          vUv = uv;
          gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
        }
      `,
            fragmentShader: `
        uniform sampler2D texture;
        uniform vec3 correction;
        varying vec2 vUv;
        void main() {
          vec4 color = texture2D(texture, vUv);
          vec3 correctedColor = mix(color.rgb, correction, 0.5);
          gl_FragColor = vec4(correctedColor, color.a);
        }
      `,
            transparent: true,
            side: THREE.DoubleSide,
        });

        const customPlane = new THREE.Mesh(geometry, customMaterial);
        customPlane.position.set(obj.x, obj.y, obj.z + 0.01); // Slightly in front of the original plane
        customPlane.renderOrder = (obj.renderOrder || 0) + 1;
        scene.add(customPlane);
    });

    // Approach 4: Adjust renderer output encoding
    //renderer.outputEncoding = THREE.sRGBEncoding;
}

/* 
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';

// 设置后处理
const composer = new EffectComposer(renderer);
const renderPass = new RenderPass(scene, camera);
composer.addPass(renderPass);

const colorCorrectionShader = {
  uniforms: {
    tDiffuse: { value: null },
    correction: { value: new THREE.Vector3(0.8901960784313725, 0.8901960784313725, 0.9254901960784314) }
  },
  vertexShader: `
    varying vec2 vUv;
    void main() {
      vUv = uv;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    }
  `,
  fragmentShader: `
    uniform sampler2D tDiffuse;
    uniform vec3 correction;
    varying vec2 vUv;
    void main() {
      vec4 color = texture2D(tDiffuse, vUv);
      vec3 correctedColor = mix(color.rgb, correction, 0.2);
      gl_FragColor = vec4(correctedColor, color.a);
    }
  `
};

const colorCorrectionPass = new ShaderPass(colorCorrectionShader);
composer.addPass(colorCorrectionPass);

// 在渲染循环中使用composer.render()代替renderer.render()
function animate() {
  requestAnimationFrame(animate);
  composer.render();
}
animate();

*/